/**
 * JAVACC DEMO 1.0
 */
package com.apache.info.service.plugins;

import com.alibaba.druid.pool.DruidDataSource;
import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.datasource.ConnctionData;
import com.apache.database.datasource.DynamicDataSource;
import com.apache.database.datasource.DynamicDataSourceHolder;
import com.apache.info.entity.PubSortInfo;
import com.apache.info.manager.InfoReleaseManager;
import com.apache.info.manager.PubSortInfoManager;
import com.apache.tools.StrUtil;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * description:  动态删除表操作
 *
 * @author Hou Dayu 创建时间：2016-8-28
 */
public class DynamicDropTablePluginImpl implements PluginConnector {

	private InfoReleaseManager infoReleaseManager;

	protected PubSortInfoManager pubSortInfoManager;

	/**
	 * TODO 动态删除表插件实现.
	 *
	 * @see PluginConnector#execute(ParamsVo)
	 */
	public Object execute(ParamsVo vo) throws Exception {
		PubSortInfo sort = (PubSortInfo) vo.getParams("sort");
		String sysName = StrUtil.doNull(sort.getSysName(), "plateform");
		vo.setKey("");
		//        vo.setParams("dyncSql", dropTableSqlForOracle(sort.getInfoTableName()));
		//        vo.setParams("sort", sort);
		DruidDataSource bds = DynamicDataSource.getInstance().getDataSource(sysName);//ConnctionData.getInstance().getDataSource(sysName);
		//		if (null == bds) {
		//			bds = ConnctionData.getInstance().getDataSource("plateform");
		//		}
		boolean mark =false;
		try {
			Connection con = bds.getConnection();
			mark = ConnctionData.getInstance().execute(con, dropTableSqlForOracle(sort.getInfoTableName()));
		}catch (SQLException e){
			DynamicDataSource.getInstance().removeDataSource(sysName);
			return false;
		}
		//if (mark) {
		DynamicDataSourceHolder.putDataSource(Validator.getDefaultStr("", "default"));
		ParamsVo<PubSortInfo> svo = new ParamsVo<PubSortInfo>();
		sort.setSortStatus("0");
		svo.setObj(sort);
		pubSortInfoManager.editInfo(svo);
		//}
		return mark;
		//        DynamicDataSourceHolder.putDataSource(Validator.getDefaultStr(sysName, "default"));

		//        return infoReleaseManager.deleteInfo(vo);
	}

	private String dropTableSqlForOracle(String tableName) {
		//alter   table   表名  drop  column   列名
		String sql = "drop table " + tableName;
		return sql;
	}

	public void setInfoReleaseManager(InfoReleaseManager infoReleaseManager) {
		this.infoReleaseManager = infoReleaseManager;
	}

	/**
	 * pubSortInfoManager.
	 *
	 * @param pubSortInfoManager the pubSortInfoManager to set
	 */
	public void setPubSortInfoManager(PubSortInfoManager pubSortInfoManager) {
		this.pubSortInfoManager = pubSortInfoManager;
	}

}
